<?php

namespace App\Admin\Controllers\Vote\Player;

use App\Admin\Actions\Batches\BatchDisable;
use App\Admin\Actions\Batches\BatchEnable;
use App\Admin\Actions\Player\BatchUpgradePoster;
use App\Admin\Actions\Player\UpgradeLink;
use App\Admin\Actions\Player\UpgradePoster;
use App\Admin\Renders\CommonGrid;
use App\Admin\Traits\WithUploads;
use App\Models\Chapter;
use App\Models\Player;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Database\Eloquent\Builder;

class PlayerController extends AdminController
{
    use WithUploads;

    protected string $title = '选手管理';

    public function grid(): Grid
    {
        $model = Player::ofAdmin()
            ->with(['chapter', 'season'])
            ->when(request()->season_id, function (Builder $query, $id) {
                $query->where('season_id', $id);
            })
            ->latest();

        return Grid::make($model, function (Grid $grid) {
            if (Admin::user()->isAdministrator()) {
                $grid->column('application.name', '所属应用');
                $grid->disableCreateButton();
                $grid->disableActions();
            }
            $grid->disableBatchDelete(false);
            $grid->batchActions([
                new BatchUpgradePoster(),
                new BatchEnable(Player::class),
                new BatchDisable(Player::class),
            ]);
            $grid->actions([
                new UpgradePoster,
                new UpgradeLink,
            ]);

            $grid->quickSearch(['name'])
                ->placeholder('选手名称');

            $grid->column('season.title', '赛季名称');
            $grid->column('chapter.title', '活动场次');

            CommonGrid::player($grid);
        });
    }

    public function form(): Form
    {
        return Form::make(Player::ofAdmin(), function (Form $form) {
            $form->block(8, function (Form\BlockForm $form) {
                $form->title('基础信息');
                $form->showFooter();
                $form->select('chapter_id', '活动场次')
                    ->required()
                    ->options(Chapter::ofAdmin()->ofEnabled()->pluck('title', 'id'));
                $form->text('name', '选手姓名')
                    ->required();
                $form->switch('status')
                    ->default(true);
                $form->editor('description', '选手介绍')
                    ->imageUrl(route('admin.upload.editor'));
            });
            $form->block(4, function (Form\BlockForm $form) {
                $form->title('图片信息');
                $this->cover($form, 'cover', '选手头像')
                    ->dimensions(['min_width' => 375, 'max_width' => 750])
                    ->help('图片宽度请在375~750px之间')
                    ->required();
                $this->pictures($form)
                    ->dimensions(['min_width' => 375, 'max_width' => 750])
                    ->help('图片宽度请在375~750px之间');
            });
        });
    }
}
